package com.atguigu.gmall.product.init;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.eneity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
public class InitTask implements SpringApplicationRunListener {

    /**
     * 项目创建就启动
     */
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    SkuInfoService skuInfoService;

    public void initBitMap(){

        log.info("正在初始化 :skuids:bitmap");
        List<SkuInfo> list = skuInfoService.lambdaQuery().select(SkuInfo::getId).list();
        list.stream().forEach(item ->{
            stringRedisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP,item.getId(),true);
        });
        log.info("初始化skuid:bitmap完成");
    }

    SpringApplication application;
    public InitTask(SpringApplication application , String[] args){
        log.info("监听器对象创建");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {

        log.info("监听到项目：started");
    }

    @Override
    public void starting() {

        log.info("监听到项目：starting");
    }
}
